<?php
$FORREADING = 1;
$FORWRITING = 2;
$FORAPPENDING = 8;

$YYYYDMMDDD = "YYYY-MM-DD";
$YYYYDMMDDDSHHCHHCSS = "DD-MM-YYYY HH:MM:SS";

$PRODUCTNAME = "Politie Narrowcast";

$THECONNECTION = 'mysql:host=localhost;dbname=narrowbase';
$THEREADCONNECTION = "DSN=kabelread;";
$THEARCHIVECONNECTION = 'mysql:host=localhost;dbname=narrowarchive';
$THEONLINECONNECTION = 'mysql:host=localhost;dbname=narrowonline';
$LOG = '/var/log/apache2/';
#' the first running server is considered the master server
# TODO:
# if application("narrowcast.server") = "" then application("narrowcast.server") = request.serverVariables("REMOTE_HOST")

$username = "narrow";
$password = "narrow";
$options = array(
	PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES utf8',
	PDO::ATTR_PERSISTENT => true
);
$tmpConnection = new PDO($THECONNECTION, $username, $password, $options);
$readConnection = null;
$archiveConnection = null;
$onlineConnection = null;

function closeDatabase() {
	$tmpConnection = null;
}

function openArchive() {
	global $THEARCHIVECONNECTION;
	$archiveConnection = new PDO($THEARCHIVECONNECTION, $username, $password, $options);
}

function closeArchive() {
	$archiveConnection = null;
}

function openOnline() {
	global $THEONLINECONNECTION;
	$onlineConnection = new PDO($THEONLINECONNECTION, $username, $password, $options);
}

function closeOnline() {
	$onlineConnection = null;
}

function normalize($s) {
	// Normalize line endings
	// Convert all line-endings to UNIX format
	$s = str_replace("\r\n", "\n", $s);
	$s = str_replace("\r", "\n", $s);
	// Don't allow out-of-control blank lines
	$s = preg_replace("/\n{2,}/", "\n\n", $s);
	return $s;
}

function replaceLineEndings($s) {
	return str_replace("\n", "<br />", normalize($s));
}

function makeParam($txt) {
	$result = "";
	$inp = explode('<br>', $txt);
	if ($inp[0] !== $txt) {
		foreach ($inp as $t) {
			$tmpArr = explode('=', $t, 2);
			$ub = count($tmpArr);
			if ($ub >= 1) {
				if ($ub == 1) {
					$tmpArr[1] = "";
				}
				$result = $result . '&' . urlencode($tmpArr[0]) . '=' . urlencode($tmpArr[1]);
			}
		}
	}
	return $result;
}

function makeFollowUpArray($array) {
	$followUp = array();
	for ($i = 0; $i < (count($array) - 1);
		$i++) {
		$followUp[$array[$i]] = $array[$i + 1];
	}
	$followUp[$array[count($array) - 1]] = $array[0];
	return $followUp;
}

function asFormattedDate($theDate) {
	return substr($theDate, 6, 2) . '-' . substr($theDate, 4, 2);
}

function asDatabaseDate($theDate) {
	$dateArray = explode("-", $theDate);
	return sprintf("%04s%02s%02s", $dateArray[2], $dateArray[1], $dateArray[0]);
}

function array_insert(&$array, $element, $position = null) {
	if (count($array) == 0) {
		$array[] = $element;
	} elseif (is_numeric($position) && $position < 0) {
		if ((count($array) + position) < 0) {
			$array = array_insert($array, $element, 0);
		} else {
			$array[count($array) + $position] = $element;
		}
	} elseif (is_numeric($position) && isset($array[$position])) {
		$part1 = array_slice($array, 0, $position, true);
		$part2 = array_slice($array, $position, null, true);
		$array = array_merge($part1, array($position => $element), $part2);
		foreach ($array as $key => $item) {
			if (is_null($item)) {
				unset($array[$key]);
			}
		}
	} elseif (is_null($position)) {
		$array[] = $element;
	} elseif (!isset($array[$position])) {
		$array[$position] = $element;
	}
	$array = array_merge($array);
	return $array;
}

?>